package com.xuptGurantee.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuptGurantee.system.dto.ActivityDto;
import com.xuptGurantee.system.entity.Activity;
import com.xuptGurantee.system.entity.Matcher;
import com.xuptGurantee.system.service.ActivityDtoService;
import com.xuptGurantee.system.service.ActivityService;
import com.xuptGurantee.system.service.MatcherService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ActivityDtoServiceImpl implements ActivityDtoService {

    @Resource
    MatcherService matcherService;

    @Resource
    ActivityService activityService;

    @Override
    public List<ActivityDto> getActDtoList(ActivityDto activityDto) {

        LambdaQueryWrapper<Matcher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Matcher::getVolunteerId, activityDto.getVolunteerId());
        List<Matcher> matcherList = matcherService.list(queryWrapper);

        LambdaQueryWrapper<Activity> actWrapper = new LambdaQueryWrapper<>();
        actWrapper.like(Activity::getActivityTitle, activityDto.getActivityName());
        actWrapper.eq(Activity::getActivityStatus, 1);
        List<Activity> activityList = new ArrayList<>();
        if (activityDto.getAllOrOwn().equals("own")) {
            for (Matcher match : matcherList
            ) {
                LambdaQueryWrapper<Activity> ownActWrapper = new LambdaQueryWrapper<>();
                ownActWrapper.eq(Activity::getActivityStatus, 1);
                ownActWrapper.eq(Activity::getId,match.getActivityId());
                ownActWrapper.like(Activity::getActivityTitle, activityDto.getActivityName());
                activityList.add(activityService.getOne(ownActWrapper));
            }
        } else {
            activityList = activityService.list(actWrapper);
        }
        List<ActivityDto> activityDtos = new ArrayList<>();
        for (Activity act : activityList) {
            ActivityDto actD = new ActivityDto();
            if (act != null) {
                BeanUtils.copyProperties(act, actD);
                if (matcherList.size() != 0) {
                    for (Matcher match : matcherList
                    ) {
                        if (act.getId().equals(match.getActivityId()) || (actD.getIsEngaged() != null && actD.getIsEngaged().equals("true"))) {
                            actD.setIsEngaged("true");
                        } else {
                            actD.setIsEngaged("false");
                        }
                    }
                } else {
                    actD.setIsEngaged("false");
                }
                activityDtos.add(actD);

            }
        }
        return activityDtos;


    }


}
